//
//  ViewController.m
//  Review-Objective-C
//
//  Created by mac on 2017/7/18.
//  Copyright © 2017年 com.qiankun. All rights reserved.
//

#import "ViewController.h"
#import "TSView.h"
#import "cellWithAnimation.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property(nonatomic,strong) UITableView *tableView;

//定义使用展示动画的参数
@property (nonatomic, assign) CGFloat lastScrollOffset;
/**设置cell角度 */
@property (nonatomic, assign) CGFloat angle;
/**设置cell锚点 */
@property (nonatomic, assign) CGPoint cellAnchorPoint;

@property (nonatomic, strong) UITableView *leftTableview;

@property (nonatomic, strong) UITableView *rightTableview;


@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSIndexPath *currentSelected;



@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setTableviewWithInterrelated];
    
    
}

-(void)selectedleftTableViewWithScrollView: (UIScrollView *)scrollView{
    if (self.currentSelected) return;
    if ((UITableView *) scrollView == self.leftTableview) return;
   
    [self.leftTableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.rightTableview.indexPathsForVisibleRows.firstObject.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        
    
}

#pragma mark DataSouce
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == self.leftTableview) return 1;
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTableview) return self.dataArray.count;
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    
    if (tableView == self.leftTableview) {
        cell.textLabel.text = self.dataArray[indexPath.row];
    }else{
        cell.textLabel.text = [NSString
            stringWithFormat:@"%@ ----- 第%zd行", self.dataArray[indexPath.section], indexPath.row + 1];
    }
    return  cell;
}
//delegate
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (tableView == self.leftTableview) return nil;
    return self.dataArray[section];
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"底部注解文字";
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.rightTableview) return;
    
    [self.rightTableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
    
    self.currentSelected = indexPath;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == self.leftTableview) return 0;
    return 30;
}

//设置联动tbaleview
-(void)setTableviewWithInterrelated{

    _leftTableview =
        [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 120, 500)];
    _rightTableview =
        [[UITableView alloc] initWithFrame:CGRectMake(200, 0, 300, 500)];
    
    [self.view addSubview:_leftTableview];
    [self.view addSubview:_rightTableview];
    
    self.leftTableview.delegate = self;
    self.leftTableview.dataSource = self;
    self.rightTableview.delegate = self;
    self.rightTableview.dataSource = self;

        [self.leftTableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}


// 监听上下滚动.加载不同的动画
-(void)scrollViewDidScroll:(UITableView *)scrollView{
     [self selectedleftTableViewWithScrollView:scrollView];
    
    
    
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    // 重新选中一下当前选中的行数，不然会有bug
    if (self.currentSelected) self.currentSelected = nil;

}
//demo1
-(void)showCellWithAT:(UITableView *)tableView Indexpath:(NSIndexPath *) indexPath{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 300, 400) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    cellWithAnimation *cell = [cellWithAnimation cellFromXib:tableView cellAnchorPoint:_cellAnchorPoint angle:_angle];
    if (indexPath.row % 2 == 0) {
        cell.backgroundColor = [UIColor colorWithRed:0.29 green:0.95 blue:0.63 alpha:1.00];
    }else{
        cell.backgroundColor = [UIColor colorWithRed:0.40 green:0.22 blue:0.94 alpha:1.00];
    }
}
//demo2
-(void)showcell:( NSIndexPath *) indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; cell.textLabel.text = @"我最帅";
    if (indexPath.row % 2 == 1) {
        UIView *v = [[UIView alloc] init];
        v.backgroundColor = [UIColor blueColor];
        cell.backgroundView = v;
    }
}

/*
 demo3
 使用多线程调度任务
 DISPATCH_QUEUE_CONCURRENT ==> 并行队列
 DISPATCH_QUEUE_SERIAL 《==》NULL ==> 串行队列
 sync  同步
 async 异步
*/

-(void)useGCDWithasync
{
    dispatch_queue_t gcd =  dispatch_queue_create("com.qiankun.test", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(gcd, ^{
        NSLog(@"1");
    });
    dispatch_async(gcd, ^{
        NSLog(@"2");
    });
    
    dispatch_async(gcd, ^{
        NSLog(@"3");
    });
    dispatch_async(gcd, ^{
        NSLog(@"4");
    });
}


-(void)useGCDWithsync
{
    
    dispatch_queue_t gcd =  dispatch_queue_create(0, 0);
    
    dispatch_async(gcd, ^{
        NSLog(@"1 --- %@",[NSThread currentThread]);
    });

    dispatch_async(gcd, ^{
        NSLog(@"2--- %@",[NSThread currentThread]);
    });
    
    dispatch_async(gcd, ^{
        for (int i = 0; i < 9; i ++) {
            NSLog(@"%d-%@",i,[NSThread currentThread]);
        }
        NSLog(@"4-%@",[NSThread currentThread]);
        
    });
}

/*
 使用notification 传值
 1. 创建一个通知, 字典
 2. 字典用来存储信息
 3. 通知 -> 注册 -> 接受 ->销毁
 */
- (IBAction)useNation:(UIButton *)sender
{

    NSDictionary *dict =@{@"info" : @"This Info is save the word",};
    
    NSNotification *natification = [NSNotification notificationWithName:@"showInfo" object:nil userInfo:dict];
    
    [[NSNotificationCenter defaultCenter] postNotification:natification];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)addImgWithshadows{
    TSView *v = [TSView new];
    v.frame = CGRectMake(10, 10, 200, 200);
    [self.view addSubview:v];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//加载数据
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        for (NSInteger i = 0; i<=10; i++) {
            [_dataArray addObject:[NSString stringWithFormat:@"第%zd分区",i]];
        }
    }
    return _dataArray;
}
@end
